﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

/**
 * 创建时间：2015/1/17 12:33:43
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common.Converter
{
    public class LastPartNameConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = string.Empty;
            string currentValue = string.Empty;
            try
            {
                
                if (value != null)
                    currentValue = value.ToString().Trim();
                //
                int startIndex = currentValue.IndexOf("话");
                if (startIndex >= 0)
                {
                    result = currentValue.Substring(0,startIndex+1);
                }
                if (string.IsNullOrWhiteSpace(result))
                {
                    startIndex = currentValue.IndexOf(" ");
                    if (startIndex >= 0)
                    {
                        result = currentValue.Substring(0, startIndex + 1);
                    }
                }                
            }
            catch (Exception err)
            {
            }

            if (string.IsNullOrWhiteSpace(result))
            {
                if (currentValue.Length > 5)
                    result = currentValue.Substring(0,5);
                else
                    result = currentValue;
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
}
